<template>
  <div class="upload-container">
    <!-- <el-upload
      :action="action"
      :headers="headers"
      :multiple="false"
      :limit="1"
      :before-upload="beforeUpload"
      :on-success="onSuccess"
      :on-error="onError"
      :on-remove="onRemove"
      :file-list="fileList"
      :on-exceed="onExceed"
      :disabled="disabled"
      drag
      show-file-list
      accept="application/epub+zip"
      class="image-upload"
    >
      <i class="el-icon-upload" />
      <div v-if="fileList.length === 0" class="el-upload__text">
        请假电子书拖入或<em>点击上传</em>
      </div>
      <div v-else class="el-upload__text">
        图书已上传
      </div>
    </el-upload> -->
    <!-- <div style="text-align: center;">
      请假电子书拖入或<em>点击上传</em>
      (此功能尚未开发)
    </div> -->
  </div>
</template>

<script>
import { getToken } from '../../utils/auth'

export default {
  props: {
    fileList: {
      type: Array,
      default() {
        return []
      }
    },
    disabled: {
      type: Boolean,
      default() {
        return false
      }
    }
  },
  data() {
    return {
      action: `${process.env.VUE_APP_BASE_API}/book/upload`
    }
  },
  computed: {
    headers() {
      return {
        Authorization: `Bearer ${getToken()}`
      }
    }
  },
  methods: {
    beforeUpload(file) {
      this.$emit('beforeUpload', file)
    },
    onSuccess(response, file) {
      console.log(response, file)
      const { code, msg, data } = response
      if (code === 0) {
        this.$message({
          type: 'success',
          message: msg
        })
        this.$emit('onSuccess', data)
      } else {
        this.$message({
          type: 'error',
          message: '上传失败'
        })
        this.$emit('onError', file)
      }
    },
    // eslint-disable-next-line handle-callback-err
    onError(err) {
      this.$message({
        message: `上传失败`,
        type: 'error'
      })
      this.$emit('onError', err)
    },
    onRemove() {
      this.$message({
        type: 'success',
        message: '电子书删除成功'
      })
      this.$emit('onRemove')
    },
    onExceed() {
      this.$message({
        type: 'warning',
        message: '每次只能上传一本电子书'
      })
    }
  }
}
</script>

<style lang="" scoped>
</style>
